/// 面积计算接口
pub trait Area {
    type Output;
    fn get_area(&self) -> Self::Output;
}

/// 正方形结构体定义
struct Square<T> {
    side: T,
}

/// 圆结构体定义
struct Circle<T> {
    radius: T,
}

/// 三角形结构体定义
struct Triangle<T> {
    base: T,
    hight: T,
}

/// 正方形面积计算 (长 * 宽)
impl<T: std::ops::Mul<Output = T> + Copy> Area for Square<T> {
    type Output = T;
    
    fn get_area(&self) -> Self::Output {
        self.side * self.side
    }
}

/// 实现圆面积计算 （半径 * 半径）* 圆周率
impl<T: std::ops::Mul<Output = T> + Into<f64> + Copy> Area for Circle<T> {
    type Output = f64;
    
    fn get_area(&self) -> Self::Output {
        (self.radius * self.radius).into() * std::f64::consts::PI
    }
}

/// 三角形面积计算,1/2(长*高)
impl<T: std::ops::Mul<Output = T> + Into<f64> + Copy> Area for Triangle<T> {
    type Output = f64;
    
    fn get_area(&self) -> Self::Output {
        (self.base * self.hight).into() * 0.5
    }
}
fn main() {

    let s = Square {side: 100};
    println!("正方形面积: {}", s.get_area());
    
    let r = Circle {radius: 5};
    println!("圆面积: {}", r.get_area());

    let t = Triangle {base: 5, hight: 3};
    println!("三角形面积: {}", t.get_area());
}